package com.it.controller;

import com.it.pojo.Equation;
import com.it.pojo.EquationContainer;


public class EquationController {
    private EquationContainer equationContainer;

    public EquationController() {
        this.equationContainer = new EquationContainer();

    }

    public EquationContainer getEquationContainer() {
        return equationContainer;
    }

    public void setEquationContainer(EquationContainer equationContainer) {
        this.equationContainer = equationContainer;
    }

    public boolean addToContainer(Equation equation) {
        return equationContainer.add(equation);
    }

    public int getSize() {
        return equationContainer.getSize();
    }

    public Equation[] getEquationArray() {
        return equationContainer.getEquationArray();
    }

    public int[] getMaxStingLengthForFields() {
        int[] arr = new int[3];

        for (int i = 0; i < this.getEquationArray().length; i++) {
            Integer left = this.getEquationArray()[i].getLeft();
            arr[0] = left.toString().length() > arr[1] ? left.toString().length() : arr[0];

            Integer right = this.getEquationArray()[i].getRight();
            int rightLength = right.toString().length();
            if (this.getEquationArray()[i].getOperator().equals("+") && this.getEquationArray()[i].getRight() < 0) {
                rightLength += 2;
            } else if (this.getEquationArray()[i].getOperator().equals("-") && this.getEquationArray()[i].getRight() < 0) {
                rightLength += 2;
            }

            arr[1] = rightLength > arr[1] ? rightLength : arr[1];

            Integer result = this.getEquationArray()[i].getResult();
            int resultLength = result.toString().length();
            if (result < 0) {
                result += 2;

            }
            arr[2] = resultLength > arr[2] ? resultLength : arr[2];


        }
        return arr;
    }

    public void list() {
        list(1);
    }

    public void list(int rowCount) {


        Equation[] equationArray = this.getEquationArray();


        int[] maxStingLengthForFields = this.getMaxStingLengthForFields();

        String out = "";

        int count = 1;

        for (int i = 0; i < equationArray.length; i++) {
            String space = " ";
            Integer left = equationArray[i].getLeft();
            Integer right = equationArray[i].getRight();
            Integer result = equationArray[i].getResult();
            String equation = left.toString();


            int num = maxStingLengthForFields[0] - left.toString().length();
            for (int j = 0; j < num; j++) {
                equation = space + left.toString();
            }

            equation = equation + " " + equationArray[i].getOperator() + " ";

            num = maxStingLengthForFields[1] - right.toString().length();

            if (this.getEquationArray()[i].getOperator().equals("+") && this.getEquationArray()[i].getRight() < 0) {
                num -= 2;
                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + "(" + right.toString() + ")";

            } else if (this.getEquationArray()[i].getOperator().equals("-") && this.getEquationArray()[i].getRight() < 0) {
                num -= 2;
                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + "(" + right.toString() + ")";
            } else {
                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + right;

            }

            equation = equation + " = ";
            num = maxStingLengthForFields[2] - result.toString().length();

            if (result < 0) {
                num -= 2;

                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + "(" + result + ")";


            } else {
                for (int j = 0; j < num; j++) {
                    equation = equation + space;


                }
                equation = equation + result;


                out = equation;


                if (count > 0 && (count % rowCount) == 0) {
                    System.out.println(out);

                } else {
                    System.out.print(out);
                    System.out.print("       ");
                }
                count++;

            }

        }


    }

    public void showExercise() {
        showExercise(1);

    }

    public void showExercise(int rowCount) {


        Equation[] equationArray = this.getEquationArray();


        int[] maxStingLengthForFields = this.getMaxStingLengthForFields();

        String out = "";

        int count = 1;

        for (int i = 0; i < equationArray.length; i++) {
            String space = " ";
            Integer left = equationArray[i].getLeft();
            Integer right = equationArray[i].getRight();
            Integer result = equationArray[i].getResult();
            String equation = left.toString();


            int num = maxStingLengthForFields[0] - left.toString().length();
            for (int j = 0; j < num; j++) {
                equation = space + left.toString();
            }

            equation = equation + " " + equationArray[i].getOperator() + " ";

            num = maxStingLengthForFields[1] - right.toString().length();

            if (this.getEquationArray()[i].getOperator().equals("+") && this.getEquationArray()[i].getRight() < 0) {
                num -= 2;
                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + "(" + right.toString() + ")";

            } else if (this.getEquationArray()[i].getOperator().equals("-") && this.getEquationArray()[i].getRight() < 0) {
                num -= 2;
                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + "(" + right.toString() + ")";
            } else {
                for (int j = 0; j < num; j++) {
                    equation = equation + space;

                }
                equation = equation + right;

            }

            equation = equation + " = ";


            out = equation;


            if (count > 0 && (count % rowCount) == 0) {
                System.out.println(out);

            } else {
                System.out.print(out);
                System.out.print("       ");
            }
            count++;

        }

    }


}








